﻿using ApplicationModels.A04.Interactions;
using ApplicationModels.A05.Reports;
using AutoMapper;
using Commons.Responses;
using EntityModels.E05.Reports;
using IApplicationServices;
using IRepositories;
using System.Linq.Expressions;

namespace ApplicationService
{
    public class ReportTypeService : BaseApplicationService<int, ReportType, ReportTypeAM>, IReportTypeService
    {
        public ReportTypeService(IBaseRepository<int, ReportType> repository, IMapper mapper) : base(repository, mapper)
        {
        }
        public override async Task<DatasResponse<ReportTypeAM>> GetAllAsync()
        {
            var res = (await Repository.GetAllAsync(x => x.ParentId == null)).Datas;
            var parents = Mapper.Map<List<ReportTypeAM>>(res);
            var total = 0;
            foreach (var parent in parents)
            {
                var childrens = (await Repository.GetAllAsync(x => x.ParentId == parent.Id)).Datas;
                parent.Children = Mapper.Map <List<ReportTypeAM>>(childrens);
                total += childrens.Count;
            }
            return new DatasResponse<ReportTypeAM>(total,parents);
        }
    }
}
